//
//  AppDelegate.m
//  XZOA
//
//  Created by yinda on 17/5/23.
//  Copyright © 2017年 projects. All rights reserved.
//

#import "AppDelegate.h"
#import "PDRCore.h"
#import "WebViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"resultUrl"];

    [self initUI];
    return [PDRCore initEngineWihtOptions:launchOptions withRunMode:PDRCoreRunModeWebviewClient];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)initUI{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    WebViewController *vc = [[WebViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
}



#pragma mark- 从特定服务器获取新的服务器地址
- (NSURLSessionTask *)getSeverURL {
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *URL;
     URL = [NSURL URLWithString:@"http://218.77.106.72:157/oa_url.txt"];
    // 通过URL初始化task,在block内部可以直接对返回的数据进行处理
    NSURLSessionTask *task = [session dataTaskWithURL:URL
                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError* error) {
                                        
                                        if (!error) {
                                                                            
                                            NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                            NSLog(@"%@",dataString);
                                            [[NSUserDefaults standardUserDefaults]setObject:dataString forKey:@"resultUrl"];
                                    
                                        }
                                        
                                    }];
    // 启动任务
    [task resume];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [task cancel];
    });
    
    return task;
}

@end
